package com.renwei.yunlong.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.renwei.yunlong.R;
import com.renwei.yunlong.utils.UtilList;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by wangqi on 2017/11/15.
 */

public class ImageSmallFileAdapter extends BaseAdapter {

    private Context mContext;
    private List<String> mList = new ArrayList<>();
    private OnItemDeleteClickListener listener;

    public interface OnItemDeleteClickListener {
        void onDeleteClick(int position);
    }

    public List<String> getmList() {
        return mList;
    }

    public void addItem(String url) {
        mList.add(url);
        notifyDataSetChanged();
    }

    public void deleteItem(int position) {
        mList.remove(position);
        notifyDataSetChanged();
    }

    public void setOnItemDeleteClickListener(OnItemDeleteClickListener listener) {
        this.listener = listener;
    }

    public ImageSmallFileAdapter(Context context) {
        this.mContext = context;
    }

    @Override
    public int getCount() {
        return UtilList.getCount(mList) + 1;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        FileHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_small_image_file, parent, false);
            holder = new FileHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (FileHolder) convertView.getTag();
        }
        if (position == mList.size()) {
            holder.iv.setScaleType(ImageView.ScaleType.FIT_XY);
            Glide.with(mContext).load(R.mipmap.photo).into(holder.iv);
            holder.ivDelete.setVisibility(View.GONE);
        } else {
            holder.iv.setScaleType(ImageView.ScaleType.CENTER_CROP);
            String item = mList.get(position);
            Glide.with(mContext).load(item).into(holder.iv);
            holder.ivDelete.setVisibility(View.VISIBLE);
        }
        holder.ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onDeleteClick(position);
                }
            }
        });
        return convertView;
    }

    class FileHolder {
        @Bind(R.id.iv)
        ImageView iv;
        @Bind(R.id.iv_delete)
        ImageView ivDelete;

        public FileHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }

}
